﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using ParseControls;

namespace ParseOnlineAdmin
{
    /// <summary>
    /// Interaction logic for ParseHeaderView.xaml
    /// </summary>
    public partial class ParseHeaderView : UserControl
    {
        public ParseHeaderView()
        {
            InitializeComponent();
        }

        private void TextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (this.txtSearch.Text != string.Empty)
                {
                    ParseOne.InformationSending(new ParseCustomEventArgs("Create:by:Matchcode"));

                    this.txtSearch.Text = string.Empty;

                    ParseOne.InformationSending(new ParseCustomEventArgs("Panel:Close"));
                }
            }
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
            Application.Current.Shutdown(0);
        }

        private void btnMin_Click(object sender, RoutedEventArgs e)
        {
            if (Application.Current.MainWindow.WindowState == WindowState.Maximized)
            {
                Application.Current.MainWindow.WindowState = WindowState.Minimized;
            }
            else if (Application.Current.MainWindow.WindowState == WindowState.Normal)
            {
                Application.Current.MainWindow.WindowState = WindowState.Minimized;
            }
            else
            {
                Application.Current.MainWindow.WindowState = WindowState.Normal;
            }
        }

        private void btnMax_Click(object sender, RoutedEventArgs e)
        {
            if (Application.Current.MainWindow.WindowState == WindowState.Maximized)
            {
                Application.Current.MainWindow.WindowState = WindowState.Normal;
            }
            else
            {
                Application.Current.MainWindow.WindowState = WindowState.Maximized;
            }
        }

        private void btnAccount_Click(object sender, RoutedEventArgs e)
        {
            ParseOne.InformationSending(new ParseCustomEventArgs("Account:Show"));
        }

        // Overrides 
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            LoadAccount();

            // Refresh 
            ParseOne.Instance.InformationSend += (sender, args) =>
                {
                    if (args.IsHandled)
                        return;

                    if (args.Command == "Account:Refresh")
                    {
                        LoadAccount();
                    }
                };
        }

        private void LoadAccount()
        {
            this.imgProfile.Source = new BitmapImage(new Uri("pack://application:,,,/Content/empty.png"));

            // Name 
            this.btnAccount.Content = (ParseOne.Package.UserName != string.Empty)
                ? this.btnAccount.Content = ParseOne.Package.UserName.ToUpper()
                    : this.btnAccount.Content = "ACCOUNT";

            // Image 
            if (ParseOne.Package.ImageUrl != string.Empty)
            {
                try
                {
                    this.imgProfile.Source = new BitmapImage(new Uri(ParseOne.Package.ImageUrl));
                }
                catch (Exception ex)
                {

                }
            }
        }
    }
}
